package Controller;

import com.sap.mw.jco.*;

import java.text.*;
import java.util.*; 


public class purchaseOrderService {
	JCO.Client mConnection;
	JCO.Repository mRepository;
	
	DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
	DateFormat dateFormat2= new SimpleDateFormat("MM/dd/yyyy");
	
	public String CreatePO( String co_code, String pur_org, String pur_group,
							String vendor, String pur_mat, 
							String plant, String date, String quantity){
		String PO_NUM = new String();
		
		try {
		      // Change the logon information to your own system/user
		      mConnection =
		        JCO.createClient("400", // SAP client
		          "int111",           // userid
		          "1234",               // password
		          null,                 // language
		          "140.115.77.101",         // application server host name
		          "00");                // system number
		      mConnection.connect();
		      mRepository = new JCO.Repository("ARAsoft", mConnection);
		    }
		catch (Exception ex) {
		      ex.printStackTrace();
		}
		
		JCO.Function function = null; 
		try {
			function = this.createFunction("BAPI_PO_CREATE");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JCO.Structure PO_HDR = function.getImportParameterList().getStructure("PO_HEADER"); 
		JCO.Table PO_LITEMS = function.getTableParameterList().getTable("PO_ITEMS"); 
		JCO.Table PO_ITM_SCH = function.getTableParameterList().getTable("PO_ITEM_SCHEDULES");
		
		//header
		PO_HDR.setValue("NB", "DOC_TYPE"); 
		PO_HDR.setValue(co_code, "CO_CODE"); 
		PO_HDR.setValue(pur_org, "PURCH_ORG"); 
		PO_HDR.setValue(pur_group, "PUR_GROUP"); 
		PO_HDR.setValue(vendor, "VENDOR");
		
		//item
		PO_LITEMS.appendRow();
		PO_LITEMS.setValue( "00001", "PO_ITEM");
		PO_LITEMS.setValue( pur_mat, "PUR_MAT");
		PO_LITEMS.setValue( plant, "PLANT");
		PO_ITM_SCH.appendRow();
		PO_ITM_SCH.setValue( "00001", "PO_ITEM");
		try {
			Date da = dateFormat.parse(dateFormat.format(dateFormat2.parse(date)));
			PO_ITM_SCH.setValue( da, "DELIV_DATE");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PO_ITM_SCH.setValue( quantity, "QUANTITY");
		
		//executing the BAPI 
		mConnection.execute(function);
		
		//accessing export parameters 
		JCO.Table returnTable = 
					function.getTableParameterList().getTable("RETURN"); 
	
		if(returnTable.getNumRows() != 0) 
		{ 
			for (int i = 0; i < returnTable.getNumRows(); i++) 
			{ 
				returnTable.setRow(i); 
				if (! (returnTable.getString("TYPE").equals("") || 
						returnTable.getString("TYPE").equals("S") || 
						returnTable.getString("TYPE").equals("W")) ) 
				{ 
					System.out.println(returnTable.getString("MESSAGE")); 
				} 
			} 
		} 
	
		PO_NUM = function.getExportParameterList(). 
												getString("PURCHASEORDER");
		
		if (!PO_NUM.isEmpty()) {	
			PO_NUM = PO_NUM ;
			
			System.out.println(PO_NUM); 
		}else{
			PO_NUM = returnTable.getString("MESSAGE") + "\t\n failure";
		}
		
		return PO_NUM;
	}
	
	public JCO.Function createFunction(String name) throws Exception {
		 try {
		      IFunctionTemplate ft = mRepository.getFunctionTemplate(name.toUpperCase());
		      if (ft == null)
		        return null;
		      return ft.getFunction();
		    }
		 catch (Exception ex) {
		      throw new Exception("Problem retrieving JCO.Function object.");
		 }
	}
	
	 public static void main(String[] args){
		 purchaseOrderService pos = new purchaseOrderService();
		 String po_No = pos.CreatePO("1000", "1000", "001", "0000001000",
				 					 "100-300", "1000", "06/10/2011", "18");
		 System.out.print(po_No);
	 }
}